package com.newgrand.tasks;

import com.newgrand.domain.model.PurchasePlan;
import com.newgrand.domain.model.Reconciliation;
import com.newgrand.service.MonthlyValuationsService;
import com.newgrand.service.PurchasePlanService;
import com.newgrand.service.ReconciliationService;
import com.newgrand.service.SettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @创建人：ZhaoFengjie
 * @修改人：ZhaoFengjie
 * @创建时间：14:28 2022/10/11
 * @修改时间:：14:28 2022/10/11
 */
@Component
public class TasksJob {


    @Resource
    private PurchasePlanService planService;
    @Resource
    private ReconciliationService reconciliationService;
    @Resource
    private MonthlyValuationsService monthlyValuationsService;
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Resource
    private SettleService settleService;

    /**
     * 定时获取采购计划数据（易龙）
     */
    @Async(value = "asyncServiceExecutor")
    //@Scheduled(cron = "")
    void syncPurchasePlan() {
        List<String> orgIdList = new ArrayList<>();
        for (String item : orgIdList) {
            getDataByOrgId(item);
        }
    }

//    /**
//     * 定时获取对账单数据（易龙）
//     */
//    @Async(value = "asyncServiceExecutor")
//    @Scheduled(cron = "0 0 0/3 * * ?")
//    void syncReconciliation() {
//        List<String> orgIdList = jdbcTemplate.queryForList("select user_mcxmid from project_table where bill_flg = '1' and app_status  = '1'", String.class);
//        for (String item : orgIdList) {
//            getReconciliationByOrgId(item);
//        }
//    }

    /**
     * 定时获取结算数据（易龙）
     */
//    @Async(value = "asyncServiceExecutor")
//    @Scheduled(cron = "0 0 0/3 * * ?")
//    void syncReconciliation() {
//        List<String> orgIdList = jdbcTemplate.queryForList("select user_mcxmid from project_table where bill_flg = '1' and app_status  = '1'", String.class);
//        for (String item : orgIdList) {
//            settleService.getData(item);
//        }
//    }

    /**
     * 定时获取月度计价数据（梦成）
     */
    @Async(value = "asyncServiceExecutor")
    @Scheduled(cron = "0 0 0/3 * * ?")
    void syncMonthlyValuations() {
        List<Map<String, Object>> orgIdList = jdbcTemplate.queryForList("select a.user_mcid,b.user_mcxmid from pcm3_cnt_m a\n" +
                "left join project_table b on a.phid_pc = b.phid \n" +
                "where a.cnt_type  = '758220811000002' and a.chk_flg  = '1'  " +
                "and a.user_mcid is not null ");
        for (Map<String, Object> item : orgIdList) {
            String user_mcxmid = item.get("user_mcxmid").toString();
            String user_mcid = item.get("user_mcid").toString();
            getMonthlyValuationsService(user_mcxmid, user_mcid);
        }
    }

    /**
     * 根据组织机构id增量获取采购计划数据
     *
     * @param orgId 组织机构id
     */

    private void getDataByOrgId(String orgId) {
        List<PurchasePlan.PurchasePlanData> dataList = planService.getData(orgId);
        planService.saveBatch(dataList, orgId);
    }

    /**
     * 根据组织机构id增量获取对账单数据
     *
     * @param orgId 组织机构id
     */
    private void getReconciliationByOrgId(String orgId) {
        List<Reconciliation.ReconciliationData> dataList = reconciliationService.getData(orgId);
        reconciliationService.saveBatch(dataList, orgId);
    }

    /**
     * 根据组织机构id增量获取对账单数据
     *
     * @param orgId 组织机构id
     */
    private void getMonthlyValuationsService(String orgId, String subcontractId) {
        monthlyValuationsService.saveDataByOrgId(orgId, subcontractId);
    }
}
